/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RecipeView.java
 *
 * Created on 2012-feb-02, 16:05:50
 */
package laboration2.gui;

import java.util.List;
import se.chalmers.ait.dat215.lab2.Ingredient;
import se.chalmers.ait.dat215.lab2.Recipe;

/**
 *
 * @author Barber
 */
public class RecipeView extends javax.swing.JPanel {

    /** Creates new form RecipeView */
    public RecipeView() {
        initComponents();
    }
    
    public void setRecipe(Recipe recipe) {
        nameLabel.setText(recipe.getName());
        servingsLabel.setText(recipe.getServings()+" portioner");
        
        priceLabel.setText("ca "+recipe.getPrice()+" kr");
        timeLabel.setText("ca "+recipe.getTime()+" min");
        difficultyLabel.setText(recipe.getDifficulty());
        
        javax.swing.ImageIcon icon = recipe.getImage();
        if (icon != null && icon.getIconWidth() > 0 && icon.getIconHeight() > 0) {
            int width = imageLabel.getMaximumSize().width-2;
            int height = imageLabel.getMaximumSize().height-2;
            System.out.println("Icon width: "+imageLabel.getMaximumSize().width+"\n"
                    + "Icon height: "+imageLabel.getMaximumSize().height);
            
            // Method 1: use the scale functionality in Recipe
            imageLabel.setIcon(recipe.getImage(width, height));
            
            imageLabel.setText(null);
            imageLabel.setVisible(true);
            
            // Method 2: uses custom class RecipeIcon to scale nicely.
            //RecipeIcon scaledIcon = new RecipeIcon(icon.getImage());
            //scaledIcon.scaleOptimal(width, height);
            //imageLabel.setIcon(scaledIcon);
        } else {
            imageLabel.setVisible(false);
        }
        ingredientsTextPane.setText(ingrListToText(recipe.getIngredients()));
        descriptionTextPane.setText(recipe.getDescription());
    }
    
    /**
     * Format a list of ingredients as a string.
     * @return A string in the form "amount unit name\namount unit name\n..."
     */
    private String ingrListToText(List<Ingredient> ingredients) {
        // If empty list
        if (ingredients == null) return null;
        
        StringBuilder builder = new StringBuilder();
        int count = 1;
        for (Ingredient ingr : ingredients) {
            String line = ingr.getAmount()+" "+ingr.getUnit()+" "+ingr.getName();
            if (count != ingredients.size()) {
                line += "\n";
                count++;
            }
            builder.append(line);
        }
        return builder.toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        servingsLabel = new javax.swing.JLabel();
        imageLabel = new javax.swing.JLabel();
        scrollPane = new javax.swing.JScrollPane();
        ingredientsTextPane = new javax.swing.JTextPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextPane = new javax.swing.JTextPane();
        priceLabel = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        difficultyLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        nameLabel.setFont(nameLabel.getFont().deriveFont(nameLabel.getFont().getStyle() | java.awt.Font.BOLD, nameLabel.getFont().getSize()+3));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(laboration2.Laboration2App.class).getContext().getResourceMap(RecipeView.class);
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        servingsLabel.setText(resourceMap.getString("servingsLabel.text")); // NOI18N
        servingsLabel.setName("servingsLabel"); // NOI18N

        imageLabel.setText(resourceMap.getString("imageLabel.text")); // NOI18N
        imageLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        imageLabel.setMaximumSize(new java.awt.Dimension(128, 128));
        imageLabel.setName("imageLabel"); // NOI18N
        imageLabel.setPreferredSize(new java.awt.Dimension(128, 128));

        scrollPane.setName("scrollPane"); // NOI18N

        ingredientsTextPane.setEditable(false);
        ingredientsTextPane.setText(resourceMap.getString("ingredientsTextPane.text")); // NOI18N
        ingredientsTextPane.setName("ingredientsTextPane"); // NOI18N
        scrollPane.setViewportView(ingredientsTextPane);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        descriptionTextPane.setEditable(false);
        descriptionTextPane.setText(resourceMap.getString("descriptionTextPane.text")); // NOI18N
        descriptionTextPane.setName("descriptionTextPane"); // NOI18N
        jScrollPane1.setViewportView(descriptionTextPane);

        priceLabel.setIcon(resourceMap.getIcon("priceLabel.icon")); // NOI18N
        priceLabel.setText(resourceMap.getString("priceLabel.text")); // NOI18N
        priceLabel.setName("priceLabel"); // NOI18N

        timeLabel.setIcon(resourceMap.getIcon("timeLabel.icon")); // NOI18N
        timeLabel.setText(resourceMap.getString("timeLabel.text")); // NOI18N
        timeLabel.setName("timeLabel"); // NOI18N

        difficultyLabel.setText(resourceMap.getString("difficultyLabel.text")); // NOI18N
        difficultyLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        difficultyLabel.setName("difficultyLabel"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(nameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 178, Short.MAX_VALUE)
                        .add(difficultyLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(timeLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(priceLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(servingsLabel)
                    .add(layout.createSequentialGroup()
                        .add(scrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(imageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(nameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(servingsLabel))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(priceLabel)
                        .add(difficultyLabel)
                        .add(timeLabel)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(scrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(imageLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane descriptionTextPane;
    private javax.swing.JLabel difficultyLabel;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JTextPane ingredientsTextPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JLabel servingsLabel;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration//GEN-END:variables
}
